/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdh;

import java.sql.SQLException;
import javax.swing.ComboBoxModel;

/**
 *
 * @author tale
 */
public class LineBoxModel extends LineModel implements ComboBoxModel{
    
    int selectedRow = 0;
    
    public LineBoxModel(){
        super();
    }

    public LineBoxModel(String select){
        super(select);
    }
    
    private int getItemRow(String item){
        try{
            rs.beforeFirst();
            while (rs.next()){
                // 注意，
                if (rs.getString(1).compareTo(item) == 0)
                    return rs.getRow() - 1;

            }
            return 0;
        }catch(SQLException ex){
            System.out.println(ex.toString());
            return 0;
        }
    }

    public void setSelectedItem(Object anItem) {
        selectedRow = getItemRow(anItem.toString());
    }


    public Object getSelectedItem() {
        return getElementAt(selectedRow);
    }

    public static LineBoxModel getPlanBoxModel(){
        String select = "SELECT NAME FROM PLANS ORDER BY ID DESC";
        return new LineBoxModel(select);
    }

    public static LineBoxModel getLectureBoxModel(){
        String select = "SELECT NAME FROM LECTURES ORDER BY ID DESC";
        return new LineBoxModel(select);
    }

    public static LineBoxModel getTeacherBoxModel(){
        String select = "SELECT NAME FROM TEACHERS ORDER BY ID DESC";
        return new LineBoxModel(select);
    }

    public static void main(String[] args){
        LineBoxModel.setBackend(Backend.getDefaultBackend());
        LineBoxModel.getLectureBoxModel().refresh();
        LineBoxModel.getTeacherBoxModel().refresh();
    }

}
